﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Lswweb.Model;
using Lswweb.IDAL;

namespace Lswweb.SqlServerDAL
{
    public class SubscribeDAL : ISubscribe
    {
        #region 成员字段及构造方法

        private Builder<SubscribeInfo> subscribeBuilder = new Builder<SubscribeInfo>("Subscribe", "Id");

        public SubscribeDAL()
        {
            subscribeBuilder.CreateModel += new ReaderBind<SubscribeInfo>(subscribeBuilder_CreateModel);
            subscribeBuilder.GetParameters += new ParametersBuilder<SubscribeInfo>(subscribeBuilder_GetParameters);
        }

        IDataParameter[] subscribeBuilder_GetParameters(SubscribeInfo t)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("@Content",string.IsNullOrEmpty(t.Content)?null:t.Content),
                DataAccess.CreateParameter("@Email",string.IsNullOrEmpty(t.Email)?null:t.Email),
                DataAccess.CreateParameter("@Fax",string.IsNullOrEmpty(t.Fax)?null:t.Fax),
                DataAccess.CreateParameter("@LinkMan",string.IsNullOrEmpty(t.LinkMan)?null:t.LinkMan),
                DataAccess.CreateParameter("@Mobile",string.IsNullOrEmpty(t.Mobile)?null:t.Mobile),
                DataAccess.CreateParameter("@Phone",string.IsNullOrEmpty(t.Phone)?null:t.Phone),
                DataAccess.CreateParameter("@Title",string.IsNullOrEmpty(t.Title)?null:t.Title),
                DataAccess.CreateParameter("@Address",string.IsNullOrEmpty(t.Address)?null:t.Address),
                DataAccess.CreateParameter("@PostalCode",string.IsNullOrEmpty(t.PostalCode)?null:t.PostalCode),
                DataAccess.CreateParameter("@Id",t.Id),
                DataAccess.CreateParameter("@IsRead",t.IsRead)};
        }

        SubscribeInfo subscribeBuilder_CreateModel(Hashtable reader)
        {
            SubscribeInfo subscribe = new SubscribeInfo();

            if (reader.ContainsKey("Content"))
                subscribe.Content = reader["Content"].ToString();

            if (reader.ContainsKey("Email"))
                subscribe.Email = reader["Email"].ToString();

            if (reader.ContainsKey("Fax"))
                subscribe.Fax = reader["Fax"].ToString();

            if (reader.ContainsKey("Id"))
                subscribe.Id = Convert.ToInt32(reader["Id"]);

            if (reader.ContainsKey("IsRead"))
                subscribe.IsRead = Convert.ToBoolean(reader["IsRead"]);

            if (reader.ContainsKey("LinkMan"))
                subscribe.LinkMan = reader["LinkMan"].ToString();

            if (reader.ContainsKey("Mobile"))
                subscribe.Mobile = reader["Mobile"].ToString();

            if (reader.ContainsKey("Phone"))
                subscribe.Phone = reader["Phone"].ToString();

            if (reader.ContainsKey("Title"))
                subscribe.Title = reader["Title"].ToString();

            if (reader.ContainsKey("DateCreated"))
                subscribe.DateCreated = Convert.ToDateTime(reader["DateCreated"]);

            if (reader.ContainsKey("Address"))
                subscribe.Address = reader["Address"].ToString();

            if (reader.ContainsKey("PostalCode"))
                subscribe.PostalCode = reader["PostalCode"].ToString();

            return subscribe;
        }

        #endregion

        #region ISubscribe 成员

        public SubscribeInfo GetSubscribe(int id)
        {
            if (id <= 0)
                return null;

            return subscribeBuilder.GetModel(id);
        }

        public IList<SubscribeInfo> GetSubscribes(bool isRead, int pageSize, int pageIndex, out int totalRecord)
        {
            string where = "IsRead=@IsRead";

            return subscribeBuilder.GetModels(string.Empty, where, "id desc", pageIndex, pageSize, out totalRecord, DataAccess.CreateParameter("IsRead",isRead));
        }

        public int Insert(SubscribeInfo subscribe)
        {
            if (subscribe == null)
                return -1;

            return subscribeBuilder.Insert(
                subscribe,
                "Title",
                "LinkMan",
                "Phone",
                "Mobile",
                "Fax",
                "Email",
                "Content",
                "Address",
                "PostalCode");
        }

        public bool Delete(int id)
        {
            if (id <= 0)
                return false;

            return subscribeBuilder.Delete(id);
        }

        public bool UpdateIsRead(int id)
        {
            if (id <= 0)
                return false;

            return subscribeBuilder.UpdateFields("IsRead",id,true);
        }

        #endregion
    }
}
